<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermissionChilden(0 ,1)">新增权限</el-button>
      <el-table default-expand-all :data="list" row-key="id" style="padding:0 20px;">
        <el-table-column prop="name" label="名称" />
        <el-table-column align="center" prop="code" label="标识" />
        <el-table-column align="center" prop="description" label="名称" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermissionChilden(row.pid, row.type, row.id, 'add')">添加</el-button>
            <el-button type="text" size="mini" @click="addPermissionChilden(row.pid, row.type, row.id, 'edit')">编辑</el-button>
            <el-button type="text" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗组件 -->
    <addPermission :form="form" @showDialog="getData" />
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission.js'
import { ListToTree } from '@/utils'
import addPermission from './components/add-permission.vue'

export default {
  name: 'Permission',
  components: {
    addPermission
  },
  data() {
    return {
      list: [],
      form: {
        pid: '',
        type: '',
        id: '',
        tip: '',
        isShow: false
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.list = ListToTree(await getPermissionList(), 0)
    },
    showDialog() {
      this.form.isShow = true
    },
    // 接收showDialog值
    getData(data) {
      this.form.isShow = data
      if (!data) {
        this.form = {
          pid: '',
          type: '',
          id: '',
          tip: '',
          isShow: false
        }
      }
    },
    addPermissionChilden(pid, type, id = 0, tip = null) {
      this.form.pid = pid
      this.form.type = type
      this.form.id = id
      this.form.tip = tip
      this.showDialog()
    }
  }
}
</script>

<style scoped lang="scss">
.btn-add {
  margin: 10px;
}
</style>
